// http://smashingcoding.com/2007/11/24/plus-loin-avec-prototypejs/
// Show all the objects in current view (helped by hexLocaltor)
var LGDSPlayGround = Class.create({
  // FIXME ? replace tiles by view argument ?
  initialize: function(PGDomName, hexLocator, view) {
    // Playground dom element
    this.PGElement = $(PGDomName);
    this.view = view;
    this.hexLocator = hexLocator;
    // Remove the notification
    this.PGElement.update('');
    this.PGElement.setStyle({
		      width: 0,
		      backgroundColor: '#999999',
		      top: 400, // TODO must be based on playerView
		      left: 300 // TODO must be based on playerView
		      });
    // Reposition
    this.PGElement.makePositioned();
  },
/*  freeItems: function() {
    for (var i = 0; i < this.items.length; ++i) {
      this.items[i].destroy();
      this.items[i] = null;
    }
    this.items = new Array();
  },
  itemsReceived: function(transport) {
    var descs = transport.responseText.evalJSON();
    this.freeItems();
    for (var i = 0 ; i < descs.length ; i++) {
      var pos = this.hexLocator.whereInPlayground(descs[i].x, descs[i].y);
      this.items[i] = new LGDSItem (this.dom,
				    pos[0], pos[1], descs[i].z,
				    descs[i].className,
				    descs[i].id);
    }
  },
  askForItems: function() {
    // Javascript is brain-damaged, see http://news.hping.org/comp.lang.javascript.archive/0637.html
    if (!this.onSuccessHandler)
      this.onSuccessHandler = this.itemsReceived.bind(this);
    // http://blog.pascal-martin.fr/post/Un-premier-appel-Ajax-avec-Prototype
    new Ajax.Request(this.baseUrl + '/item_list.php', {
      method: 'post',
      postBody: 'x=5&y=5',
      onSuccess: this.onSuccessHandler
    });
  },*/
  show: function() {
    // FIXME
    var test = this.PGElement.childElements();
    for(var i=0; i<test.length; i++)
      test[i].remove();
    
    this.tiles = this.view.getTilesInView();
    for (var i = 0 ; i < this.tiles.length ; i++)
    {
      var pos = this.hexLocator.whereInPlayGround(this.tiles[i].field.x, this.tiles[i].field.y);
      var element = new Element("img");
      element.src = this.tiles[i].field.img;
      element.setStyle({position: 'absolute',
      left: pos[0] + 'px',
      bottom: pos[1] + 'px',
      zIndex: '1'});
      // Add a child to the DOM Hierarch
      this.PGElement.appendChild(element);
    }
  }
});
